<?

class sort extends Think {
	
	static function get($dir) {
		
		if (sort::is_utf8 ( $dir )) {
			$dir = iconv ( 'UTF-8', 'GBK', $dir );
		}
		
		$sort = array ();
		$ob_dir = opendir ( 'sort/' . $dir );
		$j = 0; //+
		while ( $file = readdir ( $ob_dir ) ) {
			if ($file == "." || $file == "..") {
				continue;
			} else {
				$file = iconv ( 'GBK', 'UTF-8', $file );
				$file = explode ( ',', $file );
				/*$sort [] = array(
				'title' => $file[0],
				'value' => $file[1],
				);*/
				$sort [$j] = array ('title' => $file [0] );
				for($i = 1; $i < count ( $file ); $i ++) :
					$sort [$j] ["value$i"] = $file [$i];
				endfor
				;
				$sort [$j] ['str'] = implode ( ',', $sort [$j] );
			
			}
			$j ++; //+
		}
		closedir ( $ob_dir );
		
		if (count ( $sort ) == 0) {
			return 0;
		} else {
			return $sort;
		}
	}
	
	static function position($string, $j = 1) {
		
		$v = explode ( '/', $string );
		$far_sort = '';
		$position = array ();
		$position [0] ['title'] = '总分类';
		$position [0] ['path'] = $v [0];
		$far_sort .= $v [0] . '|';
		
		for($i = 1; $i < count ( $v ); $i ++) {
			//			$f = $far_sort;//echo $f.'<br>';
			$far_sort .= $v [$i]; //$f = $far_sort;
			//			$a = iconv('UTF-8','GBK',"sort/$f");
			
			$vv = explode ( ',', $v [$i] );
			$position [$i] ['title'] = $vv [$j];
			$position [$i] ['path'] = $far_sort; //echo 'sort/$far_sort';
			$position [$i] ['s'] = $vv [1];
			$position [$i] ['name'] = $v [$i];
			
			$far_sort .= '|';
		}
		return $position;
	}
	
	static function find_position($farther, $string, $lan = 1) {
		
		$arr = explode ( ',', $string );
		
		$path = $farther;
		//dump($farther);
		//dump($arr);
		$s0 = sort::get ( $farther );
		//$count = count ( $s0 );
			
		for($j = 0; $j < count ( $arr ); $j ++) {
			if(count($s0)<=0)  {
				return false;
			}
			
			$vv = $arr [$j];
			for($i = 0; $i < count($s0); $i ++) {
				$v = $s0 [$i];
				if (in_array ( $vv, $v )) {
					$path .= '/' . $v ['str'];
					$s0 = sort::get ( $path );
					$i = 0;
					break;
				}
			}
		}
		//dump($path);exit;
		return sort::position ( $path );
	}
	
	static function sorting($arr, $order, $by) {
		//冒泡排序
		$temp = '';
		
		$end = count ( $arr );
		for($i = 0; $i < $end; $i ++) {
			for($j = $i; $j < $end; $j ++) {
				if (strcasecmp ( $arr [$i] [$order], $arr [$j] [$order] ) > 0 && $by == 'ASC') {
					$temp = $arr [$i];
					$arr [$i] = $arr [$j];
					$arr [$j] = $temp;
				} else if (strcasecmp ( $arr [$i] [$order], $arr [$j] [$order] ) < 0 && $by == 'DESC') {
					$temp = $arr [$i];
					$arr [$i] = $arr [$j];
					$arr [$j] = $temp;
				}
			}
		}
		return $arr;
	}
	
	static function is_utf8($string) {
		return preg_match ( '%^(?: 
		[\x09\x0A\x0D\x20-\x7E] # ASCII 
		| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte 
		| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs 
		| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte 
		| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates 
		| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 
		| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 
		| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 
		)*$%xs', $string );
	}

}
?>